﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Mvc;
using FacialClub.Helpers;
using FacialClub.Models.Entities;
using FacialClub.Models.Interfaces;
using FacialClub.Models.Repositories;
using FacialClub.Models.ViewModels;

namespace FacialClub.Areas.Admin.Controllers
{
    /// <summary>
    /// Settings
    /// </summary>
    public class SettingsController : Controller
    {
        private const int DEFAULTPAGESIZE = 1;

        readonly ISettingsRepository _settingsRepository;
        private readonly ICompanyInfoRepository _companyInfoRepository;

        /// <summary>
        /// Dependency Injection enabled constructors
        /// </summary>
        public SettingsController() : this(new SettingsRepository(),new CompanyInfoRepository()){}

        /// <summary>
        /// Notification Controller Constructor
        /// </summary>
        /// <param name="settingsRepository"></param>
        /// <param name="companyInfoRepository"></param>
        public SettingsController(ISettingsRepository settingsRepository, ICompanyInfoRepository companyInfoRepository)
        {
            _settingsRepository = settingsRepository;
            _companyInfoRepository = companyInfoRepository;
        }

        /// <summary>
        /// DashBoard
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View("DashBoard");
        }

        #region CompanyInformation
        /// <summary>
        /// Returns Company Information
        /// </summary>
        /// <returns></returns>
        public ActionResult CompanyInfo(int id = 1)
        {
            var companyInfo = _companyInfoRepository.GetCompanyInfoById(id);
            return View("CompanyInfo/Index", new CompanyInfoViewModel(companyInfo));
        }

        /// <summary>
        /// Company Info
        /// </summary>
        /// <param name="companyInfo"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult CompanyInfo(CompanyInfo companyInfo)
        {
            var creditCardNumber = Request["CompanyInfo.CreditCardNumber"];
            if (ModelState.IsValid)
            {
                if (CreditCardUtility.IsValidNumber(creditCardNumber))
                {
                    companyInfo = _companyInfoRepository.GetCompanyInfoById(1);
                    companyInfo.CreatedDate = DateTime.Now;
                    companyInfo.ModifiedDate = DateTime.Now;
                    companyInfo.IsActive = true;
                    UpdateModel(companyInfo, "CompanyInfo");
                    _companyInfoRepository.Save();
                }
                else
                {
                    var cardType = CreditCardUtility.GetCardTypeFromNumber(creditCardNumber);
                    var strCardType = (cardType == null) ? "Unknown" : cardType.ToString();
                    ModelState.AddModelError("_FORM", "You have entered an invalid card number. The card type is " + strCardType + ".");
                }
            }
            return View("CompanyInfo/Index", new CompanyInfoViewModel(companyInfo));
        }
        
        #endregion


    }
}
